package servlet;


import includes.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tlrs.FantomCalculator;
import tlrs.TLRSControl;
import data.UserController;


public class TicketPrice extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Html htmlInclude = new Html();   

	public TicketPrice() 
    {
        super();
       
    }
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp"); 
		UserController uc = UserController.getInstance();
		
		out.println(htmlInclude.setHeader("Lookup Ticket Price"));
	    rd.include (request, response);
	    
	    out.println("<div id ='content'>");
	    out.println("<div id='content-left'>");
	    out.println("<h3><img src='images/tickets.png' /> Ticket Lookups</h3><br/>");
	    
	    if(uc.isValidSession(request))
	    {
	    	String departure = (String)request.getParameter("departure");
	    	String destination = (String)request.getParameter("destination");
	    	if(departure.equals(destination))
	    	{
	    		RequestDispatcher forwardDisp = getServletContext().getRequestDispatcher("/LookUpPrice");
	    		request.setAttribute("same", true);
	    		forwardDisp.forward(request, response);
	    	}
	    	TLRSControl tickC = TLRSControl.getInstance();
	    	tickC.setStations();
	    	int departureZone = tickC.getZone(departure);
	    	int destinationZone = tickC.getZone(destination);
	    	int totalZones =0;
	    	
	    	if(departureZone>destinationZone)
	    	{
	    		totalZones = departureZone - destinationZone;	
	    	}
	    	
	    	else
	    	{
	    		totalZones = destinationZone - departureZone;		
	    	}
	    	
	    	int totalFantoms = FantomCalculator.getFantoms(totalZones);
	    	out.println("<p> The price to travel from "+departure+" to " +destination + ", will cost you " +totalFantoms+" Fantoms. </br>" +
	    			"Have a safe trip!</p>");
	    	out.println("<br /><p><a href='LookUpPrice'>Look up another price - click here</a></p>");
	    	
	    }
	    
	    out.println("</div></div>");
		out.println(htmlInclude.setFooter());
		
		
	}

}
